package com.xigua.xcar;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.RouteOverlay;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.util.CommonListUtil;

public class RoutePlan extends MapActivity {
	private static Location mLocation;
//	private LocationListener mLocationListener = null;//onResume时注册此listener，onPause时需要Remove
	private MyLocationOverlay mLocationOverlay = null;	//定位图层
//	private MKSearchListener mSearchListener = null;
	private LocationUpdateListener mListener = null;
    public static void setLocation(Location loc){
    	if(null == loc){
    		return ;
    	}
    	mLocation = loc;
    }
    
    public static Location getLocation(){
    	return mLocation;
    }
	private static final String tag = "RoutePlan";
	MapView mMapView = null;	// 地图View
//	MKSearch mSearch = null;	// 搜索模块，也可去掉地图模块独立使用
	private MKPlanNode stNode;
	private MKPlanNode enNode;
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.routeplan);
        
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//		if (app.mBMapMan == null) {
//		}else{
//			app.mBMapMan.destroy();
//		}
//		app.mBMapMan = new BMapManager(getApplication());
//		app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
//		app.mBMapMan.start();
//        // 如果使用地图SDK，请初始化地图Activity
        super.initMapActivity(app.mBMapMan);
        
        mMapView = (MapView)findViewById(R.id.bmapView);
        mMapView.setBuiltInZoomControls(true);
        //设置在缩放动画过程中也显示overlay,默认为不绘制
        mMapView.setDrawOverlayWhenZooming(true);
        
        // 初始化搜索模块，注册事件监听
//        mSearch = new MKSearch();
//        mSearchListener = new MKSearchListener(){
//
//			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
//					int error) {
//				// 错误号可参考MKEvent中的定义
//				Log.i(tag, "onGetDrivingRouteResult");
//				if (error != 0 || res == null) {
//					Toast.makeText(RoutePlan.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
//					return;
//				}
//				if(null == this || null == mMapView){
//					return ;
//				}
//				RouteOverlay routeOverlay = new RouteOverlay(RoutePlan.this, mMapView);
//			    // 此处仅展示一个方案作为示例
//			    routeOverlay.setData(res.getPlan(0).getRoute(0));
//			    mMapView.getOverlays().clear();
//			    mMapView.getOverlays().add(routeOverlay);
//			    mMapView.invalidate();
//			    
//			    mMapView.getController().animateTo(res.getStart().pt);
//			}
//
//			public void onGetTransitRouteResult(MKTransitRouteResult res,
//					int error) {
//				if (error != 0 || res == null) {
//					Toast.makeText(RoutePlan.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
//					return;
//				}
//				if(null == this || null == mMapView){
//					return ;
//				}
//				TransitOverlay  routeOverlay = new TransitOverlay (RoutePlan.this, mMapView);
//			    // 此处仅展示一个方案作为示例
//			    routeOverlay.setData(res.getPlan(0));
//			    mMapView.getOverlays().clear();
//			    mMapView.getOverlays().add(routeOverlay);
//			    mMapView.invalidate();
//			    mMapView.getController().animateTo(res.getStart().pt);
//			}
//
//			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
//					int error) {
//				if (error != 0 || res == null) {
//					Toast.makeText(RoutePlan.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
//					return;
//				}
//				if(null == this || null == mMapView){
//					return ;
//				}
//				RouteOverlay routeOverlay = new RouteOverlay(RoutePlan.this, mMapView);
//			    // 此处仅展示一个方案作为示例
//			    routeOverlay.setData(res.getPlan(0).getRoute(0));
//			    mMapView.getOverlays().clear();
//			    mMapView.getOverlays().add(routeOverlay);
//			    mMapView.invalidate();
//			    
//			    mMapView.getController().animateTo(res.getStart().pt);
//			}
//			public void onGetAddrResult(MKAddrInfo res, int error) {
//			}
//			public void onGetPoiResult(MKPoiResult res, int arg1, int arg2) {
//			}
//			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
//			}
//
//			@Override
//			public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
//				
//			}
//        };
//        mSearch.init(app.mBMapMan, mSearchListener);
        
        mLocationOverlay = new MyLocationOverlay(this, mMapView);
        mLocationOverlay.enableMyLocation(); // 启用定位
        mLocationOverlay.enableCompass();    // 启用指南针
        mMapView.getOverlays().add(mLocationOverlay);
     // 注册定位事件
//        mLocationListener = new LocationListener(){
//
//			@Override
//			public void onLocationChanged(Location location) {
//				if (location != null){
//					GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6),
//							(int)(location.getLongitude()*1e6));
//					mMapView.getController().animateTo(pt);
//			        mMapView.getOverlays().add(new MyOverlay((int)location.getLatitude(),(int)location.getLongitude())); 
//			        mMapView.invalidate();
//				}
//			}
//        };
        stNode = new MKPlanNode();
        GeoPoint stpoint = new GeoPoint(CommonListUtil.getLocationPointFromSP(this).getLatitude(), CommonListUtil.getLocationPointFromSP(this).getLontitude());
        Log.i(tag, "");
		stNode.pt = stpoint;
		enNode = new MKPlanNode();
		Intent intent = this.getIntent();
		GeoPoint enPoint = new GeoPoint(intent.getIntExtra(Constants.Latitude, 0),intent.getIntExtra(Constants.Longitude, 0));
		enNode.pt = enPoint;
		mListener = new LocationUpdateListener() {
			
			@Override
			public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
				// 错误号可参考MKEvent中的定义
				Log.i(tag, "onGetDrivingRouteResult");
				if (error != 0 || res == null) {
					Toast.makeText(RoutePlan.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
					return;
				}
				if(null == this || null == mMapView){
					return ;
				}
				RouteOverlay routeOverlay = new RouteOverlay(RoutePlan.this, mMapView);
			    // 此处仅展示一个方案作为示例
			    routeOverlay.setData(res.getPlan(0).getRoute(0));
			    mMapView.getOverlays().clear();
			    mMapView.getOverlays().add(routeOverlay);
			    mMapView.invalidate();
			    
			    mMapView.getController().animateTo(res.getStart().pt);				
			}
			
			@Override
			public void notifyLocationUpdate(String address) {
				
			}

			@Override
			public void onLocationChanged(Location location) {
				if (location != null){
					GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6),
							(int)(location.getLongitude()*1e6));
					mMapView.getController().animateTo(pt);
			        mMapView.getOverlays().add(new MyOverlay((int)location.getLatitude(),(int)location.getLongitude())); 
			        mMapView.invalidate();
				}
			}
		};
		app.drivingSearch("起点", stNode, "终点", enNode);
		
//		mMapView.getOverlays().add(new MyOverlay()); 
	}
	
	

	@Override
	protected void onPause() {
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//		app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
//		mLocationOverlay.disableMyLocation();
//        mLocationOverlay.disableCompass(); // 关闭指南针
//        mSearch.init(app.mBMapMan, null);
//		app.mBMapMan.stop();
		app.setLocationUpdateListener(null);
		super.onPause();
	}
	@Override
	protected void onResume() {
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//		app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
//		mLocationOverlay.enableMyLocation();
//        mLocationOverlay.enableCompass(); // 打开指南针
//        mSearch.init(app.mBMapMan, null);
//		app.mBMapMan.start();
		app.setLocationUpdateListener(mListener);
		app.drivingSearch("起点", stNode, "终点", enNode);
		super.onResume();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public class MyOverlay extends Overlay  
    {  
		GeoPoint geoPoint;
		public MyOverlay(int lan,int lon){
			geoPoint = new GeoPoint((int) (lan),  
	                (int) (lon));  
		}
		
        Paint paint = new Paint();  
  
        @Override  
        public void draw(Canvas canvas, MapView mapView, boolean shadow)  
        {  
            // 在天安门的位置绘制一个String  
            Point point = mapView.getProjection().toPixels(geoPoint, null);  
            canvas.drawText("★这里是天安门", point.x, point.y, paint);  
        }  
    }  

}
